//
//  NearWeiBoViewController.m
//  WXWeibo
//
//  Created by apple on 16/5/23.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "NearWeiBoViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "WeiboAnnotation.h"
#import "DataService.h"
#import "WeiboModel.h"
#import "YYModel.h"
#import "WeiboAnnotationView.h"


@interface NearWeiBoViewController ()  <MKMapViewDelegate> {
    
    CLLocationManager *_locationManager;
}

@end

@implementation NearWeiBoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _locationManager = [[CLLocationManager alloc] init];
    
    
    if (kiOS8Later) {
        [_locationManager requestWhenInUseAuthorization];
    }
    
    MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:mapView];
    
    mapView.delegate = self;
    mapView.showsUserLocation = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - MKMapViewDeleagte 

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    
    //设置显示跨度
    CLLocationCoordinate2D coordinate = userLocation.coordinate;
    MKCoordinateSpan span = MKCoordinateSpanMake(0.05, 0.05);
    
    
    
    [mapView setRegion:MKCoordinateRegionMake(coordinate, span) animated:YES];
    
    NSString *lat = [NSString stringWithFormat:@"%f", coordinate.latitude];
    NSString *lon = [NSString stringWithFormat:@"%f", coordinate.longitude];
    
    [DataService requestWithURL:@"place/nearby_timeline.json" params:@{@"lat" : lat, @"long" : lon} fileData:nil httpMethod:@"get" success:^(NSURLSessionDataTask *task, id result) {
//        NSLog(@"%@",result);
        NSArray *statuses = result[@"statuses"];
        for (NSDictionary *status in statuses) {
            WeiboModel *model = [WeiboModel yy_modelWithDictionary:status];
            
            WeiboAnnotation *annotation = [[WeiboAnnotation alloc] init];
            
            annotation.weibo = model;
            
            [mapView addAnnotation:annotation];
            
        }
        
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
//
//    WeiboAnnotation *annotation = [[WeiboAnnotation alloc] init];
//    annotation.coordinate =  CLLocationCoordinate2DMake(30.323611, 120.342222);
//    
//    [mapView addAnnotation:annotation];
    
}

- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    
    
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }
    
    static NSString *identity = @"annotation";
    
    WeiboAnnotationView *weiboView = (WeiboAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identity];
    
    if (weiboView == nil) {
        weiboView = [[WeiboAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identity];
    }
    
    
    return weiboView;
    
    
    
}

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray<MKAnnotationView *> *)views {
    
    
    for (UIView *view in views) {
        view.alpha = 0;
        view.transform = CGAffineTransformMakeScale(0.3, 0.3);
        
        [UIView animateWithDuration:0.35 animations:^{
            view.alpha = 1;
            view.transform = CGAffineTransformMakeScale(1.4, 1.4);
            
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.4 animations:^{
                view.transform = CGAffineTransformIdentity;
                
            }];
        }];
    }
    
    
    
    
}

@end
